package com.travel.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.travel.common.Result;
import com.travel.domain.entity.FoodDetails;
import com.travel.service.FoodDetailsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/food")
public class FoodDetailController {

    @Autowired
    private FoodDetailsService foodDetailsService;  // 注入Service

    // 查询所有美食详情
    @GetMapping("/list")
    public Result<List<FoodDetails>> getAllFoodDetails() {
        try {
            List<FoodDetails> foodDetailsList = foodDetailsService.list();  // 获取所有记录
            return Result.success(foodDetailsList);  // 返回查询结果
        } catch (Exception e) {
            log.error("Error retrieving all food details: ", e);
            return Result.error("Failed to retrieve food details");
        }
    }

    // 根据ID查询美食详情
    @GetMapping("/{id}")
    public Result<FoodDetails> getFoodDetailById(@PathVariable("id") Integer id) {
        try {
            // 使用 LambdaQueryWrapper 来查询
            FoodDetails foodDetails = foodDetailsService.getOne(
                    new LambdaQueryWrapper<FoodDetails>()
                            .eq(FoodDetails::getId, id)  // 查询 id 字段
            );

            if (foodDetails == null) {
                return Result.error("Food details not found");
            }
            return Result.success(foodDetails);  // 返回查询到的美食详情
        } catch (Exception e) {
            log.error("Error retrieving food details by ID: ", e);
            return Result.error("Failed to retrieve food details by ID");
        }
    }
}
